const tableName = "BillDetails";
let getAllDetails = (uid, cb) => {
  let Details = new wx.BaaS.TableObject(tableName),
    query = new wx.BaaS.Query()

  query.compare('is_delete', '=', false)
  query.compare('created_by', '=', uid)
  Details.setQuery(query).limit(1000).orderBy(['-created_at']).expand(['created_by', 'project', 'bill_type']).find()
    .then(res => cb(res))
    .catch(err => console.dir(err))
}
let getDetail = (id, cb) => {
  let Details = new wx.BaaS.TableObject(tableName)

  Details.expand(['created_by', 'project', 'bill_type']).get(id)
    .then(res => cb(res))
    .catch(err => console.dir(err))
}
let getDetails = (uid, isIncome, year, month, cb) => {
  let Details = new wx.BaaS.TableObject(tableName),
    query = new wx.BaaS.Query()

  query.compare('is_delete', '=', false)
  query.compare('created_by', '=', uid)
  if (isIncome != null) {
    query.compare('is_income', '=', isIncome)
  }
  if (year != null) {
    query.compare('year', '=', year)
  }
  if (month != null) {
    query.compare('month', '=', month)
  }
  Details.setQuery(query).limit(1000).orderBy(['-created_at']).expand(['created_by', 'project', 'bill_type']).find()
    .then(res => cb(res))
    .catch(err => console.dir(err))
}
let getDetailsByProjectId = (uid, ProjectId, cb) => {
  let Details = new wx.BaaS.TableObject(tableName),
    query = new wx.BaaS.Query()

  query.compare('created_by', '=', uid)
  query.compare('project', '=', ProjectId)
  Details.setQuery(query).limit(1000).orderBy(['-created_at']).expand(['created_by', 'project', 'bill_type']).find()
    .then(res => cb(res))
    .catch(err => console.dir(err))
}
let addDetail = (matter, amount, bill_type, remarks, detail, project, year, month, day, cb) => {
  let Types = new wx.BaaS.TableObject("BillTypes");
  let Projects = new wx.BaaS.TableObject("BillProject");
  let Type = Types.getWithoutData(bill_type.id);
  let Details = new wx.BaaS.TableObject(tableName),
    Detail = Details.create()

  Detail.set("matter", matter);
  Detail.set("amount", amount);
  Detail.set("bill_type", Type);
  Detail.set("remarks", remarks);
  Detail.set("detail", detail);
  if (project) {
    let pro = Projects.getWithoutData(project.id);
    Detail.set("project", pro);
  }
  Detail.set("year", parseInt(year));
  Detail.set("month", parseInt(month));
  Detail.set("day", parseInt(day));

  Detail.save()
    .then(res => cb(res))
    .catch(err => console.dir(err))
}
let updateDetail = (id, matter, amount, bill_type, remarks, detail, project, year, month, day, cb) => {
  let Details = new wx.BaaS.TableObject(tableName),
    Detail = Details.getWithoutData(id)

  let Types = new wx.BaaS.TableObject("BillTypes");
  let Projects = new wx.BaaS.TableObject("BillProject");
  let Type = Types.getWithoutData(bill_type.id);

  Detail.set("matter", matter);
  Detail.set("amount", amount);
  Detail.set("bill_type", Type);
  Detail.set("remarks", remarks);
  Detail.set("detail", detail);
  if (project) {
    let pro = Projects.getWithoutData(project.id);
    Detail.set("project", pro);
  } else {
    Detail.set("project", null);
  }
  Detail.set("year", parseInt(year));
  Detail.set("month", parseInt(month));
  Detail.set("day", parseInt(day));

  Detail.update()
    .then(res => cb(res))
    .catch(err => console.dir(err))
}
let deleteBillsProject = (ProjectId, cb) => {
  let Details = new wx.BaaS.TableObject(tableName)
  let query = new wx.BaaS.Query()
  // 设置查询条件（比较、字符串包含、组合等）
  //...
  query.compare('project', '=', ProjectId)
  // limit、offset 可以指定按条件查询命中的数据分页
  let records = Details.getWithoutData(query)

  // 与更新特定记录一致
  records.unset("project")

  records.update().then(res => { }, err => { })
}
let deleteDetail = (id, cb) => {
  let Details = new wx.BaaS.TableObject(tableName),
    Detail = Details.getWithoutData(id)

  Detail.set("is_delete", true);

  Detail.update()
    .then(res => cb(res))
    .catch(err => console.dir(err))

  // Details.delete(id)
  //   .then(res => cb(res))
  //   .catch(err => console.dir(err))
}
let selectDetailDate = (uid, type, cb) => {
  let Details = new wx.BaaS.TableObject(tableName),
    query = new wx.BaaS.Query()

  query.compare('is_delete', '=', false)
  query.compare('created_by', '=', uid)
  if (type == 0) {
    Details.setQuery(query).limit(1000).select(['year', 'month', 'day']).orderBy(['-year', '-month', '-day']).find()
      .then(res => cb(res))
      .catch(err => console.dir(err))
  } else if (type == 1) {
    Details.setQuery(query).limit(1000).select(['year', 'month']).orderBy(['-year', '-month']).find()
      .then(res => cb(res))
      .catch(err => console.dir(err))
  } else {
    Details.setQuery(query).limit(1000).select(['year']).orderBy(['-year']).find()
      .then(res => cb(res))
      .catch(err => console.dir(err))
  }
}
let statisticsDetailByYear = (uid, year, cb) => {
  let Details = new wx.BaaS.TableObject(tableName),
    query = new wx.BaaS.Query()

  query.compare('is_delete', '=', false)
  query.compare('created_by', '=', uid)
  query.compare('year', '=', year)
  Details.setQuery(query).limit(1000).expand(['-created_by', 'project', 'bill_type']).find()
    .then(res => cb(res))
    .catch(err => console.dir(err))
}
let statisticsDetailByMonth = (uid, year, month, cb) => {
  let Details = new wx.BaaS.TableObject(tableName),
    query = new wx.BaaS.Query()

  query.compare('is_delete', '=', false)
  query.compare('created_by', '=', uid)
  query.compare('year', '=', year)
  query.compare('month', '=', month)
  Details.setQuery(query).limit(1000).expand(['-created_by', 'project', 'bill_type']).find()
    .then(res => cb(res))
    .catch(err => console.dir(err))
}
let statisticsDetailByDay = (uid, year, month, day, cb) => {
  let Details = new wx.BaaS.TableObject(tableName),
    query = new wx.BaaS.Query()

  query.compare('is_delete', '=', false)
  query.compare('created_by', '=', uid)
  query.compare('year', '=', year)
  query.compare('month', '=', month)
  query.compare('day', '=', day)
  Details.setQuery(query).limit(1000).expand(['-created_by', 'project', 'bill_type']).find()
    .then(res => cb(res))
    .catch(err => console.dir(err))
}
module.exports = {
  getDetail,
  getAllDetails,
  getDetails,
  addDetail,
  updateDetail,
  deleteDetail,
  getDetailsByProjectId,
  deleteBillsProject,
  selectDetailDate,
  statisticsDetailByYear,
  statisticsDetailByMonth,
  statisticsDetailByDay
}